package com.example.demo.day.config.learn.designBook.part8factory.part5;

import java.lang.reflect.Constructor;

//负责生成单例的工厂类
public class SingletonFactory {

    private static Singleton singleton;
    static {
        try {
            Class cl = Class.forName(Singleton.class.getName());
            //获得无参构造
            Constructor constructor = cl.getDeclaredConstructor();
            //设置无参构造是可访问的
            constructor.setAccessible(true);
            singleton = (Singleton) constructor.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Singleton getSingleton() {
        return singleton;
    }
}
